package com.homegoods.home.fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.home.R;
import com.homegoods.home.databinding.HomeFragmentBaseBinding;
import com.homegoods.home.model.HomeGroupsModel;
import com.homegoods.home.model.NewProductModel;
import com.homegoods.home.presenters.HomeActivityFragementPresenter;
import com.homegoods.home.views.IView;
import com.homegoods.resource.ILifecycle;
import com.kedll.library.fragment.KRefreshFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页所有页面的父级fragment
 */
@Route(path = ARouterPath.PATH_FRAGMENT_HOMEACTIVITY)
public class HomeActivityFragment extends KRefreshFragment<HomeFragmentBaseBinding, HomeActivityFragementPresenter> implements IView {
    private List<ILifecycle> lifecycle = new ArrayList<>();
    @Autowired
    public HomeGroupsModel child;

    @Override
    public void onFirstUserVisible() {
        super.onFirstUserVisible();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
    }

    @Override
    public void onStart() {
        super.onStart();
        for (ILifecycle lifecycle : lifecycle) {
            lifecycle.onStart();
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        for (ILifecycle lifecycle : lifecycle) {
            lifecycle.onStop();
        }
    }

    @Override
    public void onUserInvisible() {
        super.onUserInvisible();
        for (ILifecycle lifecycle : lifecycle) {
            lifecycle.onStop();
        }
    }

    @Override
    public void onUserVisible() {
        super.onUserVisible();
        for (ILifecycle lifecycle : lifecycle) {
            lifecycle.onStart();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        for (ILifecycle lifecycle : lifecycle) {
            lifecycle.onDestroy();
        }
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initEvent(Bundle savedInstanceState) {
        super.initEvent(savedInstanceState);
        ARouter.getInstance().inject(this);
        mBinding.cHeadView.setOnItemClickListener((parent, view, position, id) -> mPresenter.onMenuClick(view, position));
        mBinding.cHeadView.setOnBannerClickListener(position -> {
            mPresenter.onTopAdClick(position);
        });
        lifecycle.add(mBinding.cHeadView);
        lifecycle.add(mBinding.newProduct);
        //请求广告数据
        mPresenter.requestAD(child);
        //请求分类数据
        mPresenter.setMenuItems(child);
        Log.d(TAG, "initEvent: child tag === > " + child.getGroupName()); //箱包服饰/宅品美食.....
    }

    @Override
    protected HomeActivityFragementPresenter getPresenter() {
        return new HomeActivityFragementPresenter(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.home_fragment_base;
    }


    @Override
    public void showLoad(String msg) {

    }

    @Override
    public void hideLoad() {

    }

    @Override
    public void loadMoreFinish(boolean emptyData, boolean hasMore) {

    }

    @Override
    public void setAdPage(List<String> pages) {
        mBinding.cHeadView.setPage(pages);
    }

    /*@Override
    public void setMenuItems(HomeClassModel menuItems) {
        mBinding.cHeadView.setMenuItems(menuItems);
    }*/

    @Override
    public void setMenuItems(HomeGroupsModel menuItems) {
        mBinding.cHeadView.setMenuItems(menuItems);
    }


    @Override
    public void setNewProductContent(List<NewProductModel> newProductContent) {
        mBinding.newProduct.setNewProductContent(newProductContent);
    }
}
